<template>
  <div>
    <!--面包屑导航区-->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!--卡片视图区域-->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" class="input-with-select" clearable
                    @clear="getOrderList">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <el-table :data="orderList" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price" width="130px"></el-table-column>
        <el-table-column label="是否付款" width="130px">
          <template v-slot="scope">
            <el-tag type="danger" v-if="scope.row.pay_status === '0'">未付款</el-tag>
            <el-tag type="success" v-else-if="scope.row.pay_status === '1'">已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send" width="130px"></el-table-column>
        <el-table-column label="下单时间" width="360px">
          <template v-slot="scope">
            {{ scope.row.create_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="210px">
          <template v-slot="scope">
            <el-tooltip class="item" effect="dark" content="编辑地址" placement="top" :open-delay="100" :enterable="false">
              <el-button type="primary" @click="showEditLocationDialog" icon="el-icon-edit" size="mini"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="物流进度" placement="top" :open-delay="100" :enterable="false">
              <el-button type="success" @click="showProgressDialog" icon="el-icon-location" size="mini"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!--分页区域-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>

    <!--显示修改地址对话框-->
    <el-dialog title="修改地址"
               :visible.sync="editLocationDialogVisible" width="30%"
               @close="editLocationDialogClosed">
      <el-form :model="editLocationForm"
               ref="editLocationFormRef"
               :rules="editLocationFormRules"
               label-width="100px">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
              :options="cityData"
              v-model="editLocationForm.address1"
              :props="{expandTrigger: 'hover'}"
              @change="handleChange">

          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editLocationForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editLocationDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editLocationSave">保存</el-button>
      </div>
    </el-dialog>

    <!--显示物流进度对话框-->
    <el-dialog title="修改地址"
               :visible.sync="progressDialogVisible" width="30%">
      <!--排序按钮区域-->
      <div class="time-sort-radio">
        排序：
        <el-radio-group v-model="timeLineReverse">
          <el-radio :label="false">倒序</el-radio>
          <el-radio :label="true">正序</el-radio>
        </el-radio-group>
      </div>
      <!--时间线区域-->
      <el-timeline :reverse="timeLineReverse">
        <el-timeline-item
            v-for="(item, i) in kuaidi.data"
            :key="i"
            :timestamp="item.time">
          {{ item.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from "./citydata";
import kuaidi from "./kuaidi";

export default {
  name: "Order",
  data() {
    return {
      // 查询信息对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 订单列表数据列表
      orderList: [],
      // 总记录数
      total: 0,
      // 显示修改地址对话框
      editLocationDialogVisible: false,
      // 修改地址对象
      editLocationForm: {},
      editLocationFormRules: {
        address1: {required: true, message: '请选择省市区/县', trigger: 'blur'},
        address2: {required: true, message: '请输入详细地址', trigger: 'blur'}
      },
      // 城市数据源
      cityData,
      // 快递数据源
      kuaidi,
      // 显示物流进度的对话框
      progressDialogVisible: false,
      // 快递时间线排序方式
      timeLineReverse: false
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    async getOrderList() {
      const {data: res} = await this.$http.get(
          'orders', {params: this.queryInfo})

      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败')
      }

      console.log(res.data)
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 分页大小发生变化
    handleSizeChange(size) {
      this.queryInfo.pagesize = size
      this.getOrderList()
    },
    // 分页页数发生变化
    handleCurrentChange(num) {
      this.queryInfo.pagenum = num
      this.getOrderList()
    },
    // 显示修改地址的对话框
    showEditLocationDialog() {
      this.editLocationDialogVisible = true
    },
    // 关闭对话框后清除数据
    editLocationDialogClosed() {
      this.$refs.editLocationFormRef.resetFields()
    },
    // 点击按钮修改地址
    editLocationSave() {

    },
    // 级联选择器中的选项变化后触发
    handleChange() {

    },
    // 显示物流进度的对话框
    async showProgressDialog() {

      // const {data:res} = await this.$http.get('/kuaidi/1106975712662')
      const res = kuaidi
      if (res.meta.status !== 200) {
        return this.$message.error('获取物流信息失败')
      }

      console.log(res.data)


      this.progressDialogVisible = true
    }

  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
.time-sort-radio {
  font-size: 16px;
  margin-bottom: 25px;
}
</style>